﻿using System;
using System.Text;
using System.Web;
using System.Linq;
using Platform.Menu;
using Platform.Permission;
using System.Collections.Generic;
using BLL.Access;
using System.Data;
using BLL;

namespace Web.ashx
{
    /// <summary>
    ///     Summary description for Menu
    /// </summary>
    public class Menu : IHttpHandler
    {
        private List<MenuItem> list = null;
        public void ProcessRequest(HttpContext context)
        {
            if (!string.IsNullOrEmpty(context.Request["menu"]))
            {

                var code = context.Request.Cookies["code"].Value;
                var menuId = Convert.ToInt32(context.Request["  "]);
                var userId = Convert.ToInt32(context.Request["userid"]); // LoginHelper.UserID;
                //string ss = GetMenuList(userId, menuId);
               // Platform.Data.TList<MenuItem> listT = MenuHelper.GetAllMenusByUser(LoginHelper.UserID, menuId);
                SpHelper helper=new SpHelper();
                //Platform.Data.TList<MenuItem> listT = helper.QR_MENU(userId);
                Platform.Data.TList<MenuItem> listT = new Platform.Data.TList<MenuItem>();
               
                var userMenu = new SqlHelper().GetUserMenu(userId,code);
                if (userMenu != null)
                {
                    MenuItem userMenuItem = new MenuItem
                    {
                        MenuID = -1,
                        BelongID = 0,
                        MenuName = "个性菜单",
                        DisplayIndex = -1,
                        ImageLink = "Images/ImgV2014/xtpzOutImg.png"
                    };
                    listT.Add(userMenuItem);
                    var userDt = new SqlHelper().GetMenus(userMenu).Tables[0];
                    for (int i = 0; i < userDt.Rows.Count; i++)
                    {
                        MenuItem menuItem = new MenuItem();
                        menuItem.BelongID = -1;
                        menuItem.DisplayIndex = userDt.Rows[i]["DISPLAYINDEX"] == null ? 0 : Convert.ToInt32(userDt.Rows[i]["DISPLAYINDEX"]);
                        menuItem.Event = Convert.ToString(userDt.Rows[i]["EVENT"]);
                        menuItem.ImageLink = Convert.ToString(userDt.Rows[i]["IMAGELINK"]);
                        menuItem.MdiChildForm = userDt.Rows[i]["MDICHILDFORM"] == null ? 0 : Convert.ToInt32(userDt.Rows[i]["MDICHILDFORM"]);
                        menuItem.MenuHeight = userDt.Rows[i]["MENUHEIGHT"] == DBNull.Value ? 0 : Convert.ToInt32(userDt.Rows[i]["MENUHEIGHT"]);
                        menuItem.MenuID = userDt.Rows[i]["MENUID"] == null ? 0 : Convert.ToInt32(userDt.Rows[i]["MENUID"]);
                        menuItem.MenuName = Convert.ToString(userDt.Rows[i]["MENUNAME"]);
                        menuItem.MenuWidth = userDt.Rows[i]["MENUWIDTH"] == DBNull.Value ? 0 : Convert.ToInt32(userDt.Rows[i]["MENUWIDTH"]);
                        menuItem.PermissionID = Convert.ToString(userDt.Rows[i]["PERMISSIONID"]);
                        int PICMODE = userDt.Rows[i]["PICMODE"] == DBNull.Value ? 0 : Convert.ToInt32(userDt.Rows[i]["PICMODE"]);
                        if (PICMODE == 0)
                        {
                            menuItem.PicMode = MenuItemPicMode.None;
                        }
                        else if (PICMODE == 1)
                        {
                            menuItem.PicMode = MenuItemPicMode.Small;
                        }
                        else if (PICMODE == 2)
                        {
                            menuItem.PicMode = MenuItemPicMode.Large;
                        }
                        menuItem.Remark = Convert.ToString(userDt.Rows[i]["REMARK"]);
                        menuItem.Status = userDt.Rows[i]["STATUS"] == null ? 0 : Convert.ToInt32(userDt.Rows[i]["STATUS"]);
                        menuItem.WindowMode = userDt.Rows[i]["WINDOWSTATE"] == null ? 0 : Convert.ToInt32(userDt.Rows[i]["WINDOWSTATE"]);
                        menuItem.WindowState = userDt.Rows[i]["WINDOWMODE"] == null ? 0 : Convert.ToInt32(userDt.Rows[i]["WINDOWMODE"]);
                        listT.Add(menuItem);
                    }
                }
                
                DataTable dt = helper.QR_MENU(userId, code).Tables[0];

                
                for (int i = 0; i < dt.Rows.Count;i++)
                {
                    MenuItem menuItem = new MenuItem();
                    menuItem.BelongID = dt.Rows[i]["BELONGID"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["BELONGID"]); //Convert.ToInt32(dt.Rows[i]["BELONGID"]);
                    menuItem.DisplayIndex = dt.Rows[i]["DISPLAYINDEX"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["DISPLAYINDEX"]);
                    menuItem.Event =Convert.ToString(dt.Rows[i]["EVENT"]) ;
                    menuItem.ImageLink = Convert.ToString(dt.Rows[i]["IMAGELINK"]);
                    menuItem.MdiChildForm = dt.Rows[i]["MDICHILDFORM"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["MDICHILDFORM"]);
                    menuItem.MenuHeight = dt.Rows[i]["MENUHEIGHT"] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i]["MENUHEIGHT"]);
                    menuItem.MenuID = dt.Rows[i]["MENUID"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["MENUID"]);
                    menuItem.MenuName = Convert.ToString(dt.Rows[i]["MENUNAME"]);
                    menuItem.MenuWidth = dt.Rows[i]["MENUWIDTH"] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i]["MENUWIDTH"]);
                    menuItem.PermissionID = Convert.ToString(dt.Rows[i]["PERMISSIONID"]);
                    int PICMODE= dt.Rows[i]["PICMODE"] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i]["PICMODE"]);
                    if(PICMODE==0)
                    {
                       menuItem.PicMode=MenuItemPicMode.None;
                    }
                    else if(PICMODE==1)
                    {
                       menuItem.PicMode=MenuItemPicMode.Small;
                    }
                    else if (PICMODE == 2) 
                    {
                       menuItem.PicMode = MenuItemPicMode.Large;
                    }
                    menuItem.Remark = Convert.ToString(dt.Rows[i]["REMARK"]);
                    menuItem.Status = dt.Rows[i]["STATUS"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["STATUS"]);
                    menuItem.WindowMode = dt.Rows[i]["WINDOWSTATE"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["WINDOWSTATE"]);
                    menuItem.WindowState = dt.Rows[i]["WINDOWMODE"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["WINDOWMODE"]);
                    listT.Add(menuItem);
                }
                list = new List<MenuItem>();
                foreach (MenuItem itemT in listT)
                {
                    list.Add(itemT);
                }

                context.Response.Write(GetMenuList(0, userId, menuId));
            }
            if (!string.IsNullOrEmpty(context.Request["msg"]))
            {
                if (!string.IsNullOrEmpty(context.Request["add"]))
                {
                    var userAct = LoginHelper.Account; // context.Request["userAct"];
                    var txtBT = context.Request["txtBT"];
                    var txtNR = context.Request["txtNR"];
                    //if (AddTiXing(userAct, txtBT, txtNR))//yuzhiqiang
                    //{
                    //    context.Response.ContentType = "text/plain";
                    //    context.Response.Write("ok");
                    //}
                    //else
                    //{
                    //    context.Response.ContentType = "text/plain";
                    //    context.Response.Write("error");
                    //}
                }
            }
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get { return false; }
        }

        private string GetMenuList(int level, int userId, int menuId)
        {
            level++;
            //var _allMenuCollection = MenuHelper.GetMenusByUser(LoginHelper.UserID, menuId);

            var _allMenuCollection = (list.Where(p => p.BelongID == menuId).OrderBy(p => p.DisplayIndex)).ToList();
            if (_allMenuCollection.Count == 0)
            {
                return string.Empty;
            }
            var sb = new StringBuilder();
            if (menuId == 0)
            {
                sb.Append("<ul class=\"dl-menu\">");
            }
            else
            {
                sb.Append("<ul class=\"dl-submenu\">");
            }
            foreach (var mi in _allMenuCollection)
            {
                if (mi.MenuID == 1)
                {
                    var i = -1;
                }
                var tmp = GetMenuList(level, userId, mi.MenuID);
                if (string.IsNullOrWhiteSpace(tmp))
                {
                    sb.Append("<li class=\"menuNor\">");
                }
                else
                {
                    sb.Append("<li class=\"menuTip\">");
                }
                if (menuId == 0)
                {
                    sb.AppendFormat("<img src=\"" + mi.ImageLink + "\" alt=\"" + mi.MenuName + "\" />");
                }

                if (!string.IsNullOrEmpty(mi.Remark) && mi.Remark.Contains("show"))
                {
                    if (!string.IsNullOrEmpty(mi.Remark) && mi.Remark.Contains("地表"))
                    {
                        //sb.AppendFormat("<label class=\"ShuiZiYuanMenuTitle\" " + SetMarinLeftByLevel("", level) + "  >(地表水)</label>");
                        sb.AppendFormat("<a url=\"" + mi.Event + "\" winMd=\"" + mi.MdiChildForm + "\" title=\"" +
                                        mi.MenuName + "\"style=\"\">" + mi.MenuName + "</a>");
                    }
                    else if (!string.IsNullOrEmpty(mi.Remark) && mi.Remark.Contains("地下"))
                    {
                        //sb.AppendFormat("<label class=\"ShuiZiYuanMenuTitle\" " + SetMarinLeftByLevel("", level) + "   >(地下水)</label>");
                        sb.AppendFormat("<a url=\"" + mi.Event + "\" winMd=\"" + mi.MdiChildForm + "\" title=\"" +
                                        mi.MenuName + "\" style=\"\">" + mi.MenuName + "</a>");
                    }
                    else if (!string.IsNullOrEmpty(mi.Remark) && mi.Remark.Contains("计划"))
                    {
                        
                        //sb.AppendFormat("<label class=\"ShuiZiYuanMenuTitle\" " + SetMarinLeftByLevel("", level) + "  >(计划水)</label>");
                        sb.AppendFormat("<a url=\"" + mi.Event + "\" winMd=\"" + mi.MdiChildForm + "\" title=\"" +
                                        mi.MenuName + "\" style=\"\">" + mi.MenuName + "</a>");
                    }
                    else
                    {
                        sb.AppendFormat("<a url=\"" + mi.Event + "\" winMd=\"" + mi.MdiChildForm + "\" title=\"" +
                                   mi.MenuName + "\">" + mi.MenuName + "</a>");
                    }
                }
                else
                {
                    sb.AppendFormat("<a url=\"" + mi.Event + "\" winMd=\"" + mi.MdiChildForm + "\" title=\"" +
                                    mi.MenuName + "\">" + mi.MenuName + "</a>");
                }
                sb.Append(tmp);
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            return sb.ToString();
        }

        private string SetMarinLeftByLevel(string defalutStr, int level)
        {
            if (level > 2)
            {


                return "style=\"margin-left:35px\"";
            }
            else
            {
                return defalutStr;
            }
        }
    }
}